package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.mapper.UserInfoMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.entity.Message;
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class ChangeInfoController {
    @Autowired
    private UserMapper ume;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @RequestMapping("/updateinfo")
    public void change(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name=request.getParameter("username");
        String email=request.getParameter("email");
        int age=Integer.valueOf(request.getParameter("age"));
        int sex=Integer.valueOf(request.getParameter("sex"));
        if(!"".equals(name)||!"".equals(email)||!"".equals(age)||!"".equals(sex)) {
            User user=ume.selectByName(name);
            int n = userInfoMapper.updateInfo(email, age, sex, name);
            int num=ume.updateuser(email,user.getId());
            if (n > 0||num>0) response.getWriter().write(JSONObject.toJSONString(Message.success("success update")));
            else response.getWriter().write(JSONObject.toJSONString(Message.failure("Failure update")));
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("Update data error!")));
    }
}
